import { 
  AnthropicMessagesResponse, 
  AnthropicCountTokensResponse, 
  AnthropicModel, 
  AnthropicModelsResponse,
  AnthropicBatch,
  AnthropicFile,
  AnthropicFilesResponse 
} from '../interfaces/anthropic-api.interface';

// Mock 消息响应数据
export const mockMessagesResponse: AnthropicMessagesResponse = {
  id: 'msg_013Zva2CMHLNnXjNJJKqJ2EF',
  type: 'message',
  role: 'assistant',
  content: [
    {
      type: 'text',
      text: '您好！我是 Claude，您的AI助手。我可以帮助您处理各种任务，包括回答问题、分析数据、编写代码、创作内容等。请告诉我您需要什么帮助？'
    }
  ],
  model: 'claude-sonnet-4-20250514',
  stop_reason: 'end_turn',
  stop_sequence: null,
  usage: {
    input_tokens: 12,
    output_tokens: 45,
    cache_creation_input_tokens: 0,
    cache_read_input_tokens: 0
  },
  container: null
};

// Mock 带思考的消息响应
export const mockMessagesWithThinkingResponse: AnthropicMessagesResponse = {
  id: 'msg_014Abc3DMILOnYjOKKLrK3FG',
  type: 'message',
  role: 'assistant',
  content: [
    {
      type: 'thinking',
      text: '用户询问了一个问题，我需要仔细思考如何回答。让我分析一下这个问题的关键点...'
    },
    {
      type: 'text',
      text: '基于我的分析，这里是我的回答...'
    }
  ],
  model: 'claude-sonnet-4-20250514',
  stop_reason: 'end_turn',
  stop_sequence: null,
  usage: {
    input_tokens: 25,
    output_tokens: 120,
    cache_creation_input_tokens: 0,
    cache_read_input_tokens: 0
  },
  container: null
};

// Mock 工具使用响应
export const mockMessagesWithToolUseResponse: AnthropicMessagesResponse = {
  id: 'msg_015Def4EMJMPnZkPLLMsL4GH',
  type: 'message',
  role: 'assistant',
  content: [
    {
      type: 'text',
      text: '我需要使用工具来获取天气信息。'
    },
    {
      type: 'tool_use',
      name: 'get_weather',
      input: {
        location: '北京',
        unit: 'celsius'
      }
    }
  ],
  model: 'claude-sonnet-4-20250514',
  stop_reason: 'tool_use',
  stop_sequence: null,
  usage: {
    input_tokens: 45,
    output_tokens: 28,
    cache_creation_input_tokens: 0,
    cache_read_input_tokens: 0
  },
  container: null
};

// Mock 令牌计数响应
export const mockCountTokensResponse: AnthropicCountTokensResponse = {
  input_tokens: 12
};

// Mock 模型数据
export const mockModels: AnthropicModel[] = [
  {
    id: 'claude-sonnet-4-20250514',
    type: 'model',
    display_name: 'Claude Sonnet 4',
    created_at: '2025-05-14T00:00:00Z'
  },
  {
    id: 'claude-haiku-4-20250514',
    type: 'model',
    display_name: 'Claude Haiku 4',
    created_at: '2025-05-14T00:00:00Z'
  },
  {
    id: 'claude-opus-4-20250514',
    type: 'model',
    display_name: 'Claude Opus 4',
    created_at: '2025-05-14T00:00:00Z'
  }
];

// Mock 模型列表响应
export const mockModelsResponse: AnthropicModelsResponse = {
  data: mockModels,
  first_id: 'claude-sonnet-4-20250514',
  last_id: 'claude-opus-4-20250514',
  has_more: false
};

// Mock 批处理数据
export const mockBatch: AnthropicBatch = {
  id: 'msgbatch_013Zva2CMHLNnXjNJJKqJ2EF',
  type: 'message_batch',
  processing_status: 'in_progress',
  request_counts: {
    processing: 85,
    succeeded: 10,
    errored: 3,
    canceled: 0,
    expired: 2
  },
  created_at: '2025-01-31T10:30:00.000000Z',
  expires_at: '2025-02-01T10:30:00.000000Z',
  ended_at: null,
  results_url: null,
  metadata: {
    customer_id: 'user_123',
    batch_description: 'Monthly report generation'
  }
};

// Mock 已完成的批处理
export const mockCompletedBatch: AnthropicBatch = {
  id: 'msgbatch_014Abc3DMILOnYjOKKLrK3FG',
  type: 'message_batch',
  processing_status: 'ended',
  request_counts: {
    processing: 0,
    succeeded: 95,
    errored: 5,
    canceled: 0,
    expired: 0
  },
  created_at: '2025-01-30T15:20:00.000000Z',
  expires_at: '2025-01-31T15:20:00.000000Z',
  ended_at: '2025-01-30T18:45:00.000000Z',
  results_url: 'https://api.anthropic.com/v1/messages/batches/msgbatch_014Abc3DMILOnYjOKKLrK3FG/results',
  metadata: {
    customer_id: 'user_456',
    batch_description: 'Content moderation batch'
  }
};

// Mock 文件数据
export const mockFiles: AnthropicFile[] = [
  {
    id: 'file_013Zva2CMHLNnXjNJJKqJ2EF',
    type: 'file',
    purpose: 'batch',
    filename: 'batch_requests.jsonl',
    size_bytes: 15420,
    created_at: '2025-01-31T09:15:00.000000Z'
  },
  {
    id: 'file_014Abc3DMILOnYjOKKLrK3FG',
    type: 'file',
    purpose: 'batch',
    filename: 'analysis_data.csv',
    size_bytes: 2048576,
    created_at: '2025-01-30T14:22:00.000000Z'
  }
];

// Mock 文件列表响应
export const mockFilesResponse: AnthropicFilesResponse = {
  data: mockFiles,
  first_id: 'file_013Zva2CMHLNnXjNJJKqJ2EF',
  last_id: 'file_014Abc3DMILOnYjOKKLrK3FG',
  has_more: false
};

// 生成动态 Mock 响应的工具函数
export class AnthropicMockGenerator {
  private static generateId(): string {
    const prefix = 'msg_';
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    let result = prefix;
    for (let i = 0; i < 25; i++) {
      result += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return result;
  }

  static generateMessagesResponse(
    inputText: string, 
    model: string = 'claude-sonnet-4-20250514',
    includeThinking: boolean = false,
    includeToolUse: boolean = false
  ): AnthropicMessagesResponse {
    const content: any[] = [];
    
    if (includeThinking) {
      content.push({
        type: 'thinking',
        text: `用户说："${inputText}"。让我思考如何最好地回应这个问题...`
      });
    }

    if (includeToolUse && inputText.includes('天气')) {
      content.push({
        type: 'text',
        text: '我需要查询天气信息来回答您的问题。'
      });
      content.push({
        type: 'tool_use',
        name: 'get_weather',
        input: { location: '北京', unit: 'celsius' }
      });
    } else {
      const responses = [
        `关于您提到的"${inputText}"，我可以为您提供详细的解释。`,
        `这是一个很好的问题关于"${inputText}"。让我来帮您分析。`,
        `我理解您询问的"${inputText}"，这里是我的回答。`,
        `基于您的问题"${inputText}"，我建议考虑以下几个方面。`
      ];
      content.push({
        type: 'text',
        text: responses[Math.floor(Math.random() * responses.length)]
      });
    }

    const inputTokens = Math.ceil(inputText.length / 4);
    const outputTokens = Math.ceil(content.reduce((sum, c) => sum + (c.text?.length || 0), 0) / 4);

    return {
      id: this.generateId(),
      type: 'message',
      role: 'assistant',
      content,
      model,
      stop_reason: includeToolUse ? 'tool_use' : 'end_turn',
      stop_sequence: null,
      usage: {
        input_tokens: inputTokens,
        output_tokens: outputTokens,
        cache_creation_input_tokens: 0,
        cache_read_input_tokens: 0
      },
      container: null
    };
  }

  static generateCountTokensResponse(text: string): AnthropicCountTokensResponse {
    return {
      input_tokens: Math.ceil(text.length / 4)
    };
  }

  static generateBatchId(): string {
    const prefix = 'msgbatch_';
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    let result = prefix;
    for (let i = 0; i < 25; i++) {
      result += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return result;
  }

  static generateFileId(): string {
    const prefix = 'file_';
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    let result = prefix;
    for (let i = 0; i < 25; i++) {
      result += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return result;
  }
}

// 错误响应模板
export const mockErrorResponses = {
  invalidRequest: {
    type: 'error',
    error: {
      type: 'invalid_request_error',
      message: 'Invalid request: missing required parameter'
    }
  },
  authenticationError: {
    type: 'error',
    error: {
      type: 'authentication_error',
      message: 'Invalid API key provided'
    }
  },
  rateLimitError: {
    type: 'error',
    error: {
      type: 'rate_limit_error',
      message: 'Rate limit exceeded. Please try again later.'
    }
  },
  notFoundError: {
    type: 'error',
    error: {
      type: 'not_found_error',
      message: 'The requested resource was not found'
    }
  }
};